Thread: |'s in function arguments

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    26

    |'s in function arguments

    I've been using PDCurses in a rogue-like project recently, and one of the functions (in PDCurses) allows me to enter an arbitrary number of arguments, separated by a |. I.E. I could call
    Code:
    attron(COLOR_PAIR(1) | A_BOLD);
    and all the output after it would be bold, and use color pair 1.

    While it's nothing I need to use at the moment, it has me wondering how exactly one would write a function to take arguments like that. Google searching for it is difficult, since it seems to ignore the | in the search text. So any information or links to a good source where I can study up on this would be great.

    Thanks,
    Timothy
    My Dev Blog - The most up-to-date info on my current projects.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It has nothing to do with calling a function. The '|' is just an operator like '+'.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It's a set of bit flags, that are OR-ed together.
    For example:
    Code:
    #define READ 0x1
    #define WRITE 0x2
    
    ...
    func(READ|WRITE)//call func with READ and WRITE flags.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Okay, so I'm guessing if I were to pass a function (1|3) it would look (in bits at least) something like this: 3010?

    If I were to pass that (1|3) as an integer to a function, could I check if a 3 was one of the arguments "OR-ed" by using
    Code:
    if(3&my_int==3)
    or something?

    Thank you for your time.
    My Dev Blog - The most up-to-date info on my current projects.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Think of it like an array, or std::vector, except the 'cells' are the individual bits of the number; Using the OR operator is effectively "adding the value to the list", eg:

    0000 0010 | 1000 0000 = 1000 0010

    It's usually a good idea to memorize the standard logical operations, as well, eg:

    OR
    -----------
    0 | 0 = 0
    0 | 1 = 1
    1 | 0 = 1
    1 | 1 = 1

    AND
    -----------
    0 & 0 = 0
    0 & 1 = 0
    1 & 0 = 0
    1 & 1 = 1

    XOR
    ------------
    0 ^ 0 = 0
    0 ^ 1 = 1
    1 ^ 0 = 1
    1 ^ 1 = 0

    NOT
    ------------
    !0 = 1
    !1 = 0

    From those you can build any other possible logical operation (eg: NAND is !(A & B), etc).

    Anyway, back to the original question, to undo the result of an OR, you would negate each bit and then AND it with the number, eg:

    A |= B (set bits of B in A)
    A &= ~B (remove bits of B in A)

    Note that '~' corresponds to a logical '!' applied to each bit.
    Last edited by Sebastiani; 11-05-2009 at 09:45 PM. Reason: funny typos

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by timmeh View Post
    Okay, so I'm guessing if I were to pass a function (1|3) it would look (in bits at least) something like this: 3010?

    If I were to pass that (1|3) as an integer to a function, could I check if a 3 was one of the arguments "OR-ed" by using
    Code:
    if(3&my_int==3)
    or something?

    Thank you for your time.
    Correct. But since bit flags don't usually overlap, you can usually just use "if(3 & my_int)", which returns true if any the lower two bits are set. That said, I have seen some weird schemes where the bits do overlap, so sometimes a "strong" test is necessary, if rarely.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    26
    Thanks Sebastiani (and everyone else for that matter)! This actually just became relevant to my game as well, now that I know what all it can be used for.

    Before I forget, what I meant in my previous post was "1010", not "3010"... miss-typed it...
    Last edited by timmeh; 11-05-2009 at 10:19 PM.
    My Dev Blog - The most up-to-date info on my current projects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  4. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 03:26 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM